package com.jiantong.admin.controller.management;

import com.jiantong.admin.job.Insert;
import com.jiantong.user.service.UserWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by ThinkPad on 2017/9/18.
 */
@Controller
public class InsertUserGov {
    @RpcConsumer
    private UserWriteService userWriteService;

    @RequestMapping("/aaaaaa")
    @ResponseBody
    public int insertUserGov(@RequestParam("file") MultipartFile file) {

        String fullName = file.getOriginalFilename().toLowerCase();
        String fileName = new File(fullName).getName();
        int dotIndex = fileName.lastIndexOf(".");
        String pp = (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
        try {
            if (pp.equals("xls")) {

                List<com.jiantong.user.dto.InsertUserGov> insertUserGovList = Insert.insertUser(file);
                if (insertUserGovList == null) {
                    return -1;
                }
                Boolean b = (Boolean) userWriteService.createOrganizationUserList(insertUserGovList).getResult();
                return 0;
            } else {
                return -1;
            }

        } catch (Exception exception) {
            exception.printStackTrace();
            return -1;
        }

    }
}
